﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace AppCash
{
    public partial class frmToday : Form
    {
        public frmToday()
        {
            InitializeComponent();
        }

        private void frmToday_Load(object sender, EventArgs e)
        {
            //设置DataGridView的显示样式
            gvList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            gvList.AutoGenerateColumns = false;
            CalcCash();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            CalcCash();
        }

        private void CalcCash()
        {
            //计算当日的销售情况
            
            DataSet ds = new DataSet();
            ZhiKeDuoServer.BLL.SaleInfo bSaleInfo = new ZhiKeDuoServer.BLL.SaleInfo();
            ds = bSaleInfo.getToday(ZhiKeDuoServer.Model.GlobalsInfo.UserName);
            gvList.DataSource = ds.Tables[0];
            if (gvList.Rows.Count > 0)
            {
                double cash = 0;
                int c = 0;
                for (int i = 0; i < gvList.Rows.Count; i++)
                {
                    double price = double.Parse(gvList.Rows[i].Cells[2].Value.ToString());
                    cash = cash + price;
                    int c1 = int.Parse(gvList.Rows[i].Cells[1].Value.ToString());
                    c = c + c1;
                }
                lblBCounts.Text = (gvList.Rows.Count - 1).ToString();
                lblGCounts.Text = c.ToString();
                lblPrice.Text = string.Format("{0:F2}", cash);
            }
        }


        private void buttonX1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sflg = new SaveFileDialog();
            sflg.Filter = "Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx";
            if (sflg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            //this.gridView1.ExportToXls(sflg.FileName);
            //NPOI.xs book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.IWorkbook book = null;
            if (sflg.FilterIndex == 1)
            {
                book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            }
            else
            {
                book = new NPOI.XSSF.UserModel.XSSFWorkbook();
            }

            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("当日营收");

            // 添加表头
            NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
            int index = 0;
            foreach (DataGridViewColumn item in this.gvList.Columns)
            {
                if (item.Visible)
                {
                    NPOI.SS.UserModel.ICell cell = row.CreateCell(index);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.String);
                    cell.SetCellValue(item.HeaderText);
                    index++;
                }
            }

            // 添加数据

            for (int i = 0; i < this.gvList.RowCount; i++)
            {
                index = 0;
                row = sheet.CreateRow(i + 1);
                foreach (DataGridViewColumn item in this.gvList.Columns)
                {
                    if (item.Visible)
                    {
                        NPOI.SS.UserModel.ICell cell = row.CreateCell(index);
                        cell.SetCellType(NPOI.SS.UserModel.CellType.String);
                        cell.SetCellValue(this.gvList.Rows[i].Cells[item.Index].ToString());
                        index++;
                    }
                }
            }
            // 写入 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            book = null;

            using (FileStream fs = new FileStream(sflg.FileName, FileMode.Create, FileAccess.Write))
            {
                byte[] data = ms.ToArray();
                fs.Write(data, 0, data.Length);
                fs.Flush();
            }

            ms.Close();
            ms.Dispose();

        }

    }
}
